'use strict';

const Controller = require('egg').Controller;

class BranchController extends Controller {
    // 列表
    async index(){
        const { ctx,app } = this;

        let data = await ctx.model.Branch.findAll({
            order:[
                ['order','ASC']
            ],
        })

        await ctx.renderTemplate({
            title:"学科列表",
            tempType: "table",
            table:{
                // 按钮
                buttons: {
                    // 新增操作
                    add: "/admin/branch/create"
                },
                // 表头
                columns:[{
                    title:"ID",
                    fixed: 'center',
                    key:"id"
                },{
                    title:"学科",
                    fixed: 'left',
                    render(item) {
                        return `
                        <h2 class="table-avatar">
                            <a href="">${item.name}</a>
                        </h2>
                        `
                    }
                },{
                    title:"排序",
                    fixed: 'center',
                    key:"order"
                },{
                    title:"创建时间",
                    fixed: 'center',
                    width: 180,
                    key:"created_time"
                },{
                    title: "操作",
                    width: 200,
                    fixed: 'center',
                    action:{
                        edit:function(id){
                            return `/admin/branch/edit/${id}`
                        },
                        delete:function(id){
                            return `/admin/branch/delete/${id}`
                        },
                    }
                }]
            },
            data
        })
    }
    // 创建表单
    async create(){
        const { ctx } = this;
        await ctx.renderTemplate({
            // 页面标题
            title: "创建学科",
            // 模板类型 form表单，table表格分页
            tempType: "form",
            // 表单配置
            form: {
                // 提交地址
                action: "/admin/branch",
                // 字段配置
                fields:[{
                    label: "学科名称",
                    type: "text",
                    name: "name",
                    placeholder: "学科名称"
                }, {
                    label: "排序",
                    type: "number",
                    name: "order",
                    placeholder: "排序"
                }]
            },
            // 新增成功跳转路径
            successUrl:"/admin/branch"
        });
    }

    // 创建逻辑
    async save(){
        const { ctx,app } = this;

        ctx.validate({
            name:{
                type:"string",
                required:true,
                desc:"学科名称"
            },
            order:{
                type:"int",
                required:false,
                defValue:0,
                desc:"排序"
            },
        })

        let { name,order } = ctx.request.body;

        let res = await app.model.Branch.create({
            name,order
        })

        ctx.apiSuccess(res)
    }

    // 编辑表单页
    async edit(){
        const { ctx,app } = this
        const id = ctx.params.id

        let data = await app.model.Branch.findOne({
            where:{
                id
            }
        })
        if(!data){
            return await ctx.pageFail('该记录不存在')
        }

        data = JSON.parse(JSON.stringify(data))

        await ctx.renderTemplate({
            id,
            title:"修改学科",
            tempType:"form",
            form:{
                // 提交地址
                action: "/admin/branch/" + id,
                // 字段配置
                fields:[{
                    label: "班级名称",
                    type: "text",
                    name: "name",
                    placeholder: "学科名称"
                }, {
                    label: "排序",
                    type: "number",
                    name: "order",
                    placeholder: "排序"
                }],
                // 默认值
                data
            },
            // 修改成功跳转路径
            successUrl:"/admin/branch"
        })
    }

    // 更新逻辑
    async update(){
        const { ctx,app } = this;
        ctx.validate({
            id:{
                type:"int",
                required:true
            },
            name:{
                type:"string",
                required:true,
                desc:"学科名称"
            },
            order:{
                type:"int",
                required:true,
                defValue:0,
                desc:"排序"
            },
        })

        let id = ctx.params.id
        let { name,order } = ctx.request.body

        let branch = await app.model.Branch.findOne({
            where:{
                id
            }
        })
        if(!branch){
            return ctx.apiFail('该记录不存在')
        }

        branch.name = name
        branch.order = order

        ctx.apiSuccess(await branch.save())

    }

    // 删除
    async delete(){
        const { ctx,app } = this;
        const id = ctx.params.id
        // 删除成绩表中与该科目相关的记录
        await app.model.Score.destroy({
            where:{
                branch_id:id
            }
        })
        await app.model.Branch.destroy({
            where:{
                id
            }
        })
        ctx.toast('删除成功','success')
        ctx.redirect('/admin/branch')
    }
}

module.exports = BranchController;
